package com.hsh.servlet.user;/**
 * @author xrkhy
 * @date 2025/6/28 10:01
 * @description
 */

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hsh.pojo.User;
import com.hsh.service.UserService;
import com.hsh.service.impl.UserServiceImpl;
import com.hsh.utils.JsonResultUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "register", value = "/register")
public class register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getReader().readLine();
        User user = JSONObject.parseObject(param,User.class);
        System.out.println(param);
        UserService userService = new UserServiceImpl();
        int i =userService.register(user);
        System.out.println(i);
        if(i==0){
            String res = JsonResultUtil.buildResult(500,"注册失败",null);
            response.getWriter().write(res);
            System.out.println("注册失败");
        }else {
            System.out.println("注册成功");
            String res =JsonResultUtil.buildResult(200,"注册成功请登录",null);
            response.getWriter().write(res);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
